//
//  PostCellFrameLayout.m
//  sensen_ios
//
//  Created by pro on 16/3/1.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "PostCellFrameLayout.h"
#import "ZKTextImgReplaceHandle.h"
#define kImgOneHeight 160
#define kImgMargin 8



@implementation PostCellFrameLayout

- (void)setModel:(PostModel *)model {
    _model = model;
    
    // 计算高度
    if (_cellType == ZKCellDetail) {
        // 1.帖子标题
        CGFloat titleWidth = SCREEN_WIDTH - 10 * 2;
        CGFloat titleHeight = [_model.subject getBoundingRectOfStringSize:CGSizeMake(titleWidth, 10000) font:18].size.height;
        _titleRect = CGRectMake(10, 10, titleWidth, titleHeight);
        
        // 2.帖子创建时间图片
        CGFloat timeImgX = CGRectGetMinX(_titleRect);
        CGFloat timeImgY = CGRectGetMaxY(_titleRect) + 5;
        CGFloat timeImgW = 20;
        CGFloat timeImgH = 20;
        _timeImgRect = CGRectMake(timeImgX, timeImgY, timeImgW, timeImgH);
        
        // 3.帖子创建时间
        CGFloat timeX = CGRectGetMaxX(_timeImgRect) + 5;
        CGFloat timeY = CGRectGetMinY(_timeImgRect);
        CGFloat timeW = 300;
        CGFloat timeH = 20;
        _timeRect = CGRectMake(timeX, timeY, timeW, timeH);
        
        // 4.内容
        CGFloat contentX = CGRectGetMinX(_titleRect);
        CGFloat contentY = CGRectGetMaxY(_timeRect) + 5;
        CGFloat contentW = SCREEN_WIDTH - 10 * 2;
//        CGFloat contentH = [_model.postContent getBoundingRectOfStringSize:CGSizeMake(contentW, 10000) font:13].size.height;
        CGFloat contentH = [ZKTextImgReplaceHandle getImgTextHeightWithText:_model.postContent regex:nil size:CGSizeMake(contentW, 10000) fontSize:16];
        contentH = (contentH < 20) ? 20 : contentH;
        _contentRect = CGRectMake(contentX, contentY, contentW, contentH);
        
        // 5.图片
        CGFloat imgX = 15;
        CGFloat imgY = CGRectGetMaxY(_contentRect) + 5;
        CGFloat imgW = SCREEN_WIDTH - 30;
        CGFloat imgH = 0;
        NSMutableArray *imgHAry = [NSMutableArray array];
        for (NSString *urlStr in _model.imgAry) {
            CGSize imgSize = [UIImage getImageSizeWithURL:urlStr];
            CGFloat fitH = (imgSize.height / imgSize.width) * SCREEN_WIDTH;
            [imgHAry addObject:@(fitH)]; // 吧图片高度存入数组中
            imgH += (fitH + kImgMargin);
        }
        _imgHeightAry = [imgHAry copy];
        _imgRect = CGRectMake(imgX, imgY, imgW, imgH);
        
        //点赞按钮
        CGFloat doFavX = SCREEN_WIDTH/2 - 15 - ([_model.likes getSizeOfString:18].width/2);
        CGFloat doFavY = CGRectGetMaxY(_imgRect) + 10;
        CGFloat doFavW = 30;
        CGFloat doFavH = 30;
        _doFavourRect = CGRectMake(doFavX, doFavY, doFavW, doFavH);
        
        CGFloat doLabX = CGRectGetMaxX(_doFavourRect) + 5;
        CGFloat doLabY = CGRectGetMaxY(_imgRect) + 15;
//        CGFloat doLabW = [_model.likes getSizeOfString:20].width;
        CGFloat doLabW = 80;
        CGFloat doLabH = 20;
        _doFavLabRect = CGRectMake(doLabX, doLabY, doLabW, doLabH);
        // 6.单元格尺寸
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat w = SCREEN_WIDTH;
        CGFloat h = CGRectGetMaxY(_imgRect) + 50;
        _cellRect = CGRectMake(x, y, w, h);

    } else {
        // 1.头像
        CGFloat iconX = 10;
        CGFloat iconY = 5;
        CGFloat iconW = 30;
        CGFloat iconH = iconW;
        _iconRect = CGRectMake(iconX, iconY, iconW, iconH);
        
        // 2.昵称
        CGFloat nickX = CGRectGetMaxX(_iconRect) + 5;
        CGFloat nickY = iconY;
        CGFloat nickW = SCREEN_WIDTH - nickX - 30;
        CGFloat nickH = iconH;
        _nickRect = CGRectMake(nickX, nickY, nickW, nickH);
        
        //加精图片
        _disImageViewRect = CGRectMake(SCREEN_WIDTH - 30, 15, 20, 20);
        // 3.内容
        CGFloat contentX = CGRectGetMinX(_iconRect);
        CGFloat contentY = CGRectGetMaxY(_nickRect) + 5;
        CGFloat contentW = SCREEN_WIDTH - 10 * 2;
        CGFloat contentH = [_model.postTitle getBoundingRectOfStringSize:CGSizeMake(contentW, 10000) font:16].size.height;
        contentH = (contentH < 20) ? 20 : contentH;
        _contentRect = CGRectMake(contentX, contentY, contentW, contentH);
        
        // 4.图片
        CGFloat imgX = 0;
        CGFloat imgY = CGRectGetMaxY(_contentRect) + 5;
        CGFloat imgW = SCREEN_WIDTH;
        CGFloat oneImgH = (SCREEN_WIDTH - kImgMargin * 4) / 3;
        CGFloat imgH = 0;
        if (_model.imgAry.count > 0) {
            imgH = (oneImgH + kImgMargin) *  ((_model.imgAry.count - 1) / 3 + 1);
        }
        _imgRect = CGRectMake(imgX, imgY, imgW, imgH);
        
        // 5.帖子创建时间图片
        CGFloat timeImgX = CGRectGetMinX(_iconRect);
        CGFloat timeImgY = CGRectGetMaxY(_imgRect) + 5;
        CGFloat timeImgW = 20;
        CGFloat timeImgH = 20;
        _timeImgRect = CGRectMake(timeImgX, timeImgY, timeImgW, timeImgH);
        
        // 6.帖子创建时间
        CGFloat timeX = CGRectGetMaxX(_timeImgRect) + 5;
        CGFloat timeY = CGRectGetMinY(_timeImgRect);
        CGFloat timeW = [_model.dateline_show getSizeOfString:13].width; //TODO: 宽度最后都应该根据字符串的长度计算
        CGFloat timeH = 20;
        _timeRect = CGRectMake(timeX, timeY, timeW, timeH);
        
        // 7-8.评论和评论图片
        CGFloat commentX;
        CGFloat commentY = timeY;
        CGFloat commentW = [_model.replies getSizeOfString:13].width; //TODO: 宽度最后都应该根据字符串的长度计算
        CGFloat commentH = timeH;
        commentX = SCREEN_WIDTH - 10 - commentW;
        _commentRect = CGRectMake(commentX, commentY, commentW, commentH);
        CGFloat commentImgX;
        CGFloat commentImgY = timeY;
        CGFloat commentImgW = 20;
        CGFloat commentImgH = timeH;
        commentImgX = CGRectGetMinX(_commentRect) - 5 - commentImgW;
        _commentImgRect = CGRectMake(commentImgX, commentImgY, commentImgW, commentImgH);
        
        // 9-10.评论和评论图片
        CGFloat favourX;
        CGFloat favourY = timeY;
        CGFloat favourW = [_model.likes getSizeOfString:13].width; //TODO: 宽度最后都应该根据字符串的长度计算
        CGFloat favourH = timeH;
        favourX = CGRectGetMinX(_commentImgRect) - 10 - favourW;
        _favourRect = CGRectMake(favourX, favourY, favourW, favourH);
        CGFloat favourImgX;
        CGFloat favourImgY = timeY;
        CGFloat favourImgW = 20;
        CGFloat favourImgH = timeH;
        favourImgX = CGRectGetMinX(_favourRect) - 5 - favourImgW;
        _favourImgRect = CGRectMake(favourImgX, favourImgY, favourImgW, favourImgH);
        
        // 11.单元格尺寸
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat w = SCREEN_WIDTH;
        CGFloat h = CGRectGetMaxY(_favourImgRect) + 5;
        _cellRect = CGRectMake(x, y, w, h);
        
    }
}

@end
